package com.itao.springboot.tesk;

import com.itao.springboot.config.SpringContextHolder;
import com.itao.springboot.service.ScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;

/**
 * @author sjt
 * @Date 2024/10/30 11:47
 */
@Slf4j
public class ScheduleTask implements Runnable {

    private final RedissonClient redissonClient;
    private final ScheduleService scheduleService;

    public ScheduleTask() {
        this.scheduleService = SpringContextHolder.getBean(ScheduleService.class);
        this.redissonClient = SpringContextHolder.getBean(RedissonClient.class);
    }


    @Override
    public void run() {
        RLock lock = redissonClient.getLock("lock:schedule:task");
        boolean flag = lock.tryLock();
//        RedissonUtils redissonUtils = HyKv.upRedisson(() -> "lock:refundQueryDevTask:");
//        boolean lock = redissonUtils.tryLock(String.valueOf(this.id));
        if (!flag) {
            log.info("没有获取到锁");
            return;
        }

        try {
            scheduleService.execute();
        } finally {
            lock.unlock();
            log.info("锁释放..");

        }
    }

}
